package com.aabte.permissionframeworklearn.permission.controller;

import com.aabte.permissionframeworklearn.permission.common.ResponseBean;
import com.aabte.permissionframeworklearn.permission.dto.DeptLevelDto;
import com.aabte.permissionframeworklearn.permission.model.SysDept;
import com.aabte.permissionframeworklearn.permission.param.DeptParam;
import com.aabte.permissionframeworklearn.permission.service.SysDeptService;
import com.aabte.permissionframeworklearn.permission.service.SysTreeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author Daniel
 */
@Slf4j
@Controller
@RequestMapping(value = "/sys/dept")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysTreeService sysTreeService;

    @GetMapping("/dept.page")
    public ModelAndView page() {
        return new ModelAndView("dept");
    }

    @PostMapping("/save.json")
    @ResponseBody
    public ResponseBean saveDept(
            @RequestBody DeptParam deptParam) {
        log.info("Start saveDept. deptParam: {}", deptParam);
        SysDept sysDept = sysDeptService.save(deptParam);
        log.info("End saveDept. sysDept: {}", sysDept);
        return ResponseBean.success(sysDept);
    }

    @GetMapping("/tree.json")
    @ResponseBody
    public ResponseBean deptTree() {
        log.info("Start deptTree.");
        List<DeptLevelDto> deptLevelDtos = sysTreeService.deptTree();
        ResponseBean result = ResponseBean.success(deptLevelDtos);
        log.info("End deptTree. result: {}", result);
        return result;
    }

    @PutMapping("/update.json")
    @ResponseBody
    public ResponseBean updateDept(
            @RequestBody DeptParam deptParam) {
        log.info("Start saveDept. deptParam: {}", deptParam);
        SysDept sysDept = sysDeptService.update(deptParam);
        log.info("End saveDept. sysDept: {}", sysDept);
        return ResponseBean.success(sysDept);
    }

    @DeleteMapping("/delete.json")
    @ResponseBody
    public ResponseBean deleteDept(@RequestParam("id") Long deptId) {
        log.info("Start deleteDept. deptId: {}", deptId);
        SysDept sysDept = sysDeptService.delete(deptId);
        log.info("End deleteDept. sysDept: {}", sysDept);
        return ResponseBean.success(sysDept);
    }


}
